<?php
namespace app\modules\Admin\models;

use Illuminate\Database\Eloquent\Model;

class dianpuwithdrawModel extends Model
{
    protected $table = 'dianpu_withdraw';
    public $primaryKey = 'id';
    public $appends = ['status_name', 'withdraw_type_name', 'withdraw_info_array', 'paidoff_evidence_image'];

    protected static $AllStatus = [
        -2=>'已取消',
        -1=>'已拒绝',
        0=>'待处理',
        1=>'已同意',
        2=>'已打款',
        3=>'已收款',
    ];



    protected static $AllWithdrawType = [
        'bank'=>'银行卡',
        'weixin'=>'微信',
        'alipay'=>'支付宝',
        'other'=>'其他'
    ];

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getAllStatus()
    {
        return self::$AllStatus;
    }

    public static function getAllWithdrawType()
    {
        return self::$AllWithdrawType;
    }

    public function getStatusNameAttribute()
    {
        $status_name = '';
        $statusCodes = array_keys(self::$AllStatus);
        if(in_array($this->attributes['status'], $statusCodes)){
            $status_name = self::$AllStatus[$this->attributes['status']];
        }
        return $status_name;
    }

    public function getWithdrawTypeNameAttribute()
    {
        $type_name = '';
        $typeCodes = array_keys(self::$AllWithdrawType);
        if(in_array($this->attributes['withdraw_type'], $typeCodes)){
            $type_name = self::$AllWithdrawType[$this->attributes['withdraw_type']];
        }
        return $type_name;
    }

    public function getWithdrawInfoArrayAttribute()
    {
        $info = [];
        if(!empty($this->attributes['withdraw_info'])){
            $info = json_decode($this->attributes['withdraw_info'], true);
        }
        return $info;
    }

    public function getPaidoffEvidenceImageAttribute()
    {
        $info = [];
        if(!empty($this->attributes['paidoff_evidence_image'])){
            $info = explode(',', $this->attributes['paidoff_evidence_image']);
        }
        return $info;
    }

    public function dianpu()
    {
        return $this->belongsTo(dianpuModel::class, 'dianpu_id', 'id')->select(['id', 'company_name', 'company_representative_name', 'phone']);
    }

    public function dianpumanager()
    {
        return $this->belongsTo(\memberModel::class, 'dianpu_manager_id', 'id')->select(['id', 'nickname', 'phone', 'avatar']);
    }

    public function member()
    {
        return $this->belongsTo(\memberModel::class, 'member_id', 'id')->select(['id', 'nickname', 'phone', 'avatar']);
    }

    public function dianpuwithdrawlogs()
    {
        return $this->hasMany(dianpuwithdrawlogModel::class, 'event_id', 'id')->orderByDesc('created_at');
    }
}

